<template>
  <div class="order-container">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="search" placeholder="搜索订单号" style="width: 220px;" clearable @clear="fetchList" />
        <el-select v-model="orderStatus" placeholder="全部订单" clearable style="width: 140px;">
          <el-option v-for="item in selectOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-button type="primary" @click="fetchList">搜索</el-button>
      </div>
      <el-table :data="tableData" border stripe :header-cell-style="{ background: '#faf5fe', color: '#6f21c8' }" height="calc(100vh - 240px)" v-loading="loading">
        <el-table-column prop="orderId" label="订单号" width="100" align="center" />
        <el-table-column label="商品信息" min-width="200" align="center">
          <template #default="{ row }">
            <div v-for="item in row.orderItemList" :key="item.id">
              {{ item.productName }} x {{ item.quantity }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额" width="120" align="center">
          <template #default="{ row }">
            ¥{{ row.orderAmount.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="paymentMethod" label="支付方式" width="100" align="center">
          <template #default="{ row }">
            {{ row.paymentMethod === 'alipay' ? '支付宝' : '微信支付' }}
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="订单状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.orderStatus)">{{ row.orderStatus }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="收货信息" min-width="200" align="center">
          <template #default="{ row }">
            <div>{{ row.shippingAddress.recipientName }} {{ row.shippingAddress.phoneNumber }}</div>
            <div>{{ row.shippingAddress.province }}{{ row.shippingAddress.city }}{{ row.shippingAddress.district }}{{ row.shippingAddress.streetAddress }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="orderCreateTime" label="创建时间" min-width="140" align="center" />
        <el-table-column label="操作" width="160" align="center" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)" v-if="row.orderStatus !== '已完成'">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="fetchList"
          @current-change="fetchList"
        />
      </div>
    </el-card>
    <OrderEditDialog
      v-model:visible="editDialogVisible"
      :order-data="currentOrder"
      @success="handleEditSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import OrderEditDialog from './components/OrderEditDialog.vue'
import { getOrderList } from '@/apis/admin/order'

const tableData = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const search = ref('')
const orderStatus = ref('')

const editDialogVisible = ref(false)
const currentOrder = ref({})

const selectOptions = [
  { value: '待支付', label: '待支付' },
  { value: '已支付', label: '已支付' },
  { value: '已完成', label: '已完成' },
  { value: '已取消', label: '已取消' }
]

// 获取订单列表
const fetchList = async () => {
  loading.value = true
  try {
    const res = await getOrderList({
      current: currentPage.value,
      size: pageSize.value,
      orderId: search.value,
      orderStatus: orderStatus.value
    })
    if (res.code === '0') {
      tableData.value = res.data.records
      total.value = res.data.total
    }
  } catch (e) {
    ElMessage.error('获取订单列表失败')
  } finally {
    loading.value = false
  }
}

// 监听搜索和筛选条件变化
watch([search, orderStatus], () => {
  currentPage.value = 1
  fetchList()
})

// 状态标签类型
const getStatusType = (status) => {
  const statusMap = {
    '待支付': 'warning',
    '已支付': 'success',
    '已完成': 'info',
    '已取消': 'danger'
  }
  return statusMap[status] || 'info'
}

// 编辑订单
const handleEdit = (row) => {
  currentOrder.value = { ...row }
  editDialogVisible.value = true
}

// 编辑成功回调
const handleEditSuccess = () => {
  fetchList()
}

onMounted(() => {
  fetchList()
})
</script>

<style lang="less" scoped>
.order-container {
  padding: 20px;
}
.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
